package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bumptech.glide.Glide;
import com.donkingliang.imageselector.utils.ImageSelectorUtils;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.GridAdapter_Photo;
import com.weiyi.mashangzhu.Landlord.Bean.PhotoBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.AboutActivity;
import com.weiyi.mashangzhu.Utils.BitmapUtils;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PhotoSelectActivity extends BaseActivity {

    List<String> photoslist = new ArrayList<>();
    @BindView(R.id.moren)
    LinearLayout moren;
    @BindView(R.id.my_gv)
    GridView myGv;
    @BindView(R.id.photo)
    LinearLayout photo;
    @BindView(R.id.img)
    ImageView img;
    String info;
    String id;
    List<String> urls=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        ButterKnife.bind(this);
        info=getIntent().getStringExtra("info");
        if (info.equals("publish")){
            initPhoto(MyApp.images);
        }
        if (info.equals("edit")){
            id=getIntent().getStringExtra("id");
            initPhoto_edit();


        }

    }
    private void initPhoto_edit() {
        moren.setVisibility(View.GONE);
        photo.setVisibility(View.VISIBLE);
        OkHttpUtils.get()
                .addParams("token", SPUtils.get(this, "token", "") + "")
                .addParams("id", id + "")
                .url(API.BaseApi_L + API.GetHouseImg)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        T.showShort(PhotoSelectActivity.this, "网络连接失败");
                    }

                    @Override
                    public void onResponse(String response) {
                        MyApp.img_url.clear();
                        MyApp.get_url.clear();
                        try {
                            JSONObject jsonobject = new JSONObject(response);
                            if (jsonobject.getInt("status") == 0) {
                                JSONArray jsonarray = jsonobject.getJSONArray("data");
                                JSONObject jsonobject2 = jsonarray.getJSONObject(0);

                                if (!jsonobject2.getString("hImg1").equals("null")) {
                                    MyApp.img_url.add(new PhotoBean("get",jsonobject2.getString("hImg1")));
                                    MyApp.get_url.add(new PhotoBean("get",jsonobject2.getString("hImg1")));
                                }
                                if (!jsonobject2.getString("hImg2").equals("null")) {
                                    MyApp.img_url.add(new PhotoBean("get",jsonobject2.getString("hImg2")));
                                    MyApp.get_url.add(new PhotoBean("get",jsonobject2.getString("hImg2")));
                                }
                                if (!jsonobject2.getString("hImg3").equals("null")) {
                                    MyApp.img_url.add(new PhotoBean("get",jsonobject2.getString("hImg3")));
                                    MyApp.get_url.add(new PhotoBean("get",jsonobject2.getString("hImg3")));
                                }
                                if (!jsonobject2.getString("hImg4").equals("null")) {
                                    MyApp.img_url.add(new PhotoBean("get",jsonobject2.getString("hImg4")));
                                    MyApp.get_url.add(new PhotoBean("get",jsonobject2.getString("hImg4")));
                                }
                                if (!jsonobject2.getString("hImg5").equals("null")) {
                                    MyApp.img_url.add(new PhotoBean("get",jsonobject2.getString("hImg5")));
                                    MyApp.get_url.add(new PhotoBean("get",jsonobject2.getString("hImg5")));
                                }
                                if (!jsonobject2.getString("hImg6").equals("null")) {
                                    MyApp.img_url.add(new PhotoBean("get",jsonobject2.getString("hImg6")));
                                    MyApp.get_url.add(new PhotoBean("get",jsonobject2.getString("hImg6")));
                                }
                                initPhoto_edit( MyApp.img_url);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x001 && data != null) {
            if (info.equals("publish")){
                //获取选择器返回的数据
                for (int i = 0; i < data.getStringArrayListExtra(
                        ImageSelectorUtils.SELECT_RESULT).size(); i++) {
                    MyApp.images.add(new PhotoBean("post",data.getStringArrayListExtra(
                            ImageSelectorUtils.SELECT_RESULT).get(i)));
                }
                initPhoto(MyApp.images);
            }else {
                //获取选择器返回的数据
                for (int i = 0; i < data.getStringArrayListExtra(
                        ImageSelectorUtils.SELECT_RESULT).size(); i++) {
                    MyApp.img_url.add(new PhotoBean("post",data.getStringArrayListExtra(
                            ImageSelectorUtils.SELECT_RESULT).get(i)));
                    MyApp.post_url.add(new PhotoBean("post",data.getStringArrayListExtra(
                            ImageSelectorUtils.SELECT_RESULT).get(i)));
                }
                initPhoto_edit( MyApp.img_url);

            }


        }
    }

    public void initPhoto(List<PhotoBean> list) {
        if (list.size() == 0) {
            moren.setVisibility(View.VISIBLE);
            photo.setVisibility(View.GONE);
        } else {
            moren.setVisibility(View.GONE);
            photo.setVisibility(View.VISIBLE);
            File file = new File(list.get(0).getUrl());
            Glide.with(this).load(file).into(img);
            List<PhotoBean> imagelist=new ArrayList<>();
            for (int i = 1; i <list.size() ; i++) {
                imagelist.add(list.get(i));
            }
            myGv.setAdapter(new GridAdapter_Photo(this,R.layout.item_photo,imagelist,"post"));

        }
    }
    public void initPhoto_edit(List<PhotoBean> list) {

        if (list.size() != 0) {
            moren.setVisibility(View.GONE);
            photo.setVisibility(View.VISIBLE);
            if (list.get(0).getInfo().equals("post")) {
                File file = new File(list.get(0).getUrl());
                Glide.with(this).load(file).into(img);
            } else {
                GlideImgManager.glideLoader(PhotoSelectActivity.this, API.BaseApi_url + list.get(0).getUrl(), R.mipmap.errer, img);
            }
            List<PhotoBean> imagelist = new ArrayList<>();
            for (int i = 1; i < list.size(); i++) {
                imagelist.add(list.get(i));
            }
            myGv.setAdapter(new GridAdapter_Photo(this, R.layout.item_photo, imagelist,"edit"));


        }else {
            moren.setVisibility(View.VISIBLE);
            photo.setVisibility(View.GONE);
        }
    }


    @OnClick({R.id.back, R.id.addphoto,R.id.delete,R.id.baocun})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.addphoto:
                //限数量的多选
                if (info.equals("publish")){
                    switch (MyApp.images.size()){
                        case 0:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 6);
                            break;
                        case 1:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 5);
                            break;
                        case 2:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 4);
                            break;
                        case 3:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 3);
                            break;
                        case 4:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 2);
                            break;
                        case 5:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 1);
                            break;
                        case 6:
                            T.showShort(this,"最多可选择6张图片");
                            break;
                    }
                }else {
                    switch ( MyApp.img_url.size()){
                        case 0:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 6);
                            break;
                        case 1:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 5);
                            break;
                        case 2:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 4);
                            break;
                        case 3:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 3);
                            break;
                        case 4:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 2);
                            break;
                        case 5:
                            ImageSelectorUtils.openPhoto(this, 0x001, false, 1);
                            break;
                        case 6:
                            T.showShort(this,"最多可选择6张图片");
                            break;
                    }
                }


                break;
            case R.id.delete:
                if (info.equals("publish")){
                    MyApp.images.remove(0);
                    initPhoto(MyApp.images);
                }else {
                    if (MyApp.get_url.size()!=0){
                        MyApp.get_url.remove(0);

                    }
                    MyApp.img_url.remove(0);
                    initPhoto_edit(MyApp.img_url);
                }

                break;
            case R.id.baocun:
                if (info.equals("publish")) {
                    if (MyApp.images.size() < 4) {
                        T.showShort(this, "最少上传4张房源图片");
                    } else if (MyApp.images.size() == 4) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.images.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.images.get(i).getUrl(), 270, 480, "photo" + i + ".png");
                            photoslist.add("photo" + i + ".png");
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg", photoslist.get(0), file1)
                                .addFile("hImg", photoslist.get(1), file2)
                                .addFile("hImg", photoslist.get(2), file3)
                                .addFile("hImg", photoslist.get(3), file4)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 4) {
                                            MyApp.img.add(jsonArray.getString(0));
                                            MyApp.img.add(jsonArray.getString(1));
                                            MyApp.img.add(jsonArray.getString(2));
                                            MyApp.img.add(jsonArray.getString(3));
                                            MyApp.img1 = jsonArray.getString(0);
                                            MyApp.img2 = jsonArray.getString(1);
                                            MyApp.img3 = jsonArray.getString(2);
                                            MyApp.img4 = jsonArray.getString(3);
                                            MyApp.img5 = "";
                                            MyApp.img6 = "";
                                            finish();
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    } else if (MyApp.images.size() == 5) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.images.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.images.get(i).getUrl(), 270, 480, "photo" + i + ".png");
                            photoslist.add("photo" + i + ".png");
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
                        File file5 = new File(Environment.getExternalStorageDirectory(), photoslist.get(4));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .addFile("hImg3", photoslist.get(2), file3)
                                .addFile("hImg4", photoslist.get(3), file4)
                                .addFile("hImg5", photoslist.get(4), file5)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 5) {
                                            MyApp.img.add(jsonArray.getString(0));
                                            MyApp.img.add(jsonArray.getString(1));
                                            MyApp.img.add(jsonArray.getString(2));
                                            MyApp.img.add(jsonArray.getString(3));
                                            MyApp.img.add(jsonArray.getString(4));
                                            MyApp.img1 = jsonArray.getString(0);
                                            MyApp.img2 = jsonArray.getString(1);
                                            MyApp.img3 = jsonArray.getString(2);
                                            MyApp.img4 = jsonArray.getString(3);
                                            MyApp.img5 = jsonArray.getString(4);
                                            MyApp.img6 = "";
                                            finish();
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    } else if (MyApp.images.size() == 6) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.images.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.images.get(i).getUrl(), 270, 480, "photo" + i + ".png");
                            photoslist.add("photo" + i + ".png");
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
                        File file5 = new File(Environment.getExternalStorageDirectory(), photoslist.get(4));
                        File file6 = new File(Environment.getExternalStorageDirectory(), photoslist.get(5));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .addFile("hImg3", photoslist.get(2), file3)
                                .addFile("hImg4", photoslist.get(3), file4)
                                .addFile("hImg5", photoslist.get(4), file5)
                                .addFile("hImg6", photoslist.get(5), file6)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 6) {
                                            MyApp.img.add(jsonArray.getString(0));
                                            MyApp.img.add(jsonArray.getString(1));
                                            MyApp.img.add(jsonArray.getString(2));
                                            MyApp.img.add(jsonArray.getString(3));
                                            MyApp.img.add(jsonArray.getString(4));
                                            MyApp.img.add(jsonArray.getString(5));
                                            MyApp.img1 = jsonArray.getString(0);
                                            MyApp.img2 = jsonArray.getString(1);
                                            MyApp.img3 = jsonArray.getString(2);
                                            MyApp.img4 = jsonArray.getString(3);
                                            MyApp.img5 = jsonArray.getString(4);
                                            MyApp.img6 = jsonArray.getString(5);
                                            finish();
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }
                }else {

                    //编辑房源
                    //增加了一张图片
                    if (MyApp.post_url.size() == 1) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.post_url.get(i).getUrl(), 270, 480, "photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                            photoslist.add("photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
//                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
//                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
//                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
//                        File file5 = new File(Environment.getExternalStorageDirectory(), photoslist.get(4));
//                    File file6 = new File(Environment.getExternalStorageDirectory(), photoslist.get(5));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
//                                .addFile("hImg2", photoslist.get(1), file2)
//                                .addFile("hImg3", photoslist.get(2), file3)
//                                .addFile("hImg4", photoslist.get(3), file4)
//                                .addFile("hImg5", photoslist.get(4), file5)
//                            .addFile("hImg6",photoslist.get(5), file6)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 1) {

                                            MyApp.post_url_finish.add(jsonArray.getString(0));
                                            for (int i = 0; i < MyApp.get_url.size(); i++) {
                                                urls.add(MyApp.get_url.get(i).getUrl());
                                            }
                                            urls.addAll(MyApp.post_url_finish);
                                            if (urls.size() < 4) {
                                                T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                                            } else {
                                                switch (urls.size()) {
                                                    case 4:
                                                        for (int i = 0; i < 2; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                    case 5:
                                                        for (int i = 0; i < 1; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                }
                                                OkHttpUtils.get()
                                                        .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                                        .addParams("id", id)
                                                        .addParams("hImg1", urls.get(0))
                                                        .addParams("hImg2", urls.get(1))
                                                        .addParams("hImg3", urls.get(2))
                                                        .addParams("hImg4", urls.get(3))
                                                        .addParams("hImg5", urls.get(4))
                                                        .addParams("hImg6", urls.get(5))
                                                        .url(API.BaseApi_L + API.UpdateHouseImg)
                                                        .build()
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onError(Request request, Exception e) {
                                                                Log.e("bbbbbbbb", "===" + e);
                                                            }

                                                            @Override
                                                            public void onResponse(String response) {
                                                                try {
                                                                    JSONObject js = new JSONObject(response);
                                                                    if (js.getInt("status") == 0) {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                        MyApp.img_url.clear();
                                                                        MyApp.get_url.clear();
                                                                        MyApp.post_url.clear();
                                                                        MyApp.post_url_finish.clear();
                                                                        finish();
                                                                    } else {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }
                                                            }
                                                        });
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }//更新了两张图片
                    else if (MyApp.post_url.size() == 2) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.post_url.get(i).getUrl(), 270, 480, "photo" + i + ".png");
                            photoslist.add("photo" + i + ".png");
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 2) {
                                            MyApp.post_url_finish.add(jsonArray.getString(0));
                                            MyApp.post_url_finish.add(jsonArray.getString(1));
                                            for (int i = 0; i < MyApp.get_url.size(); i++) {
                                                urls.add(MyApp.get_url.get(i).getUrl());
                                            }
                                            urls.addAll(MyApp.post_url_finish);
                                            if (urls.size() < 4) {
                                                T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                                            } else {
                                                switch (urls.size()) {
                                                    case 4:
                                                        for (int i = 0; i < 2; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                    case 5:
                                                        for (int i = 0; i < 1; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                }
                                                OkHttpUtils.get()
                                                        .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                                        .addParams("id", id)
                                                        .addParams("hImg1", urls.get(0))
                                                        .addParams("hImg2", urls.get(1))
                                                        .addParams("hImg3", urls.get(2))
                                                        .addParams("hImg4", urls.get(3))
                                                        .addParams("hImg5", urls.get(4))
                                                        .addParams("hImg6", urls.get(5))
                                                        .url(API.BaseApi_L + API.UpdateHouseImg)
                                                        .build()
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onError(Request request, Exception e) {
                                                                Log.e("bbbbbbbb", "===" + e);
                                                            }

                                                            @Override
                                                            public void onResponse(String response) {
                                                                try {
                                                                    JSONObject js = new JSONObject(response);
                                                                    if (js.getInt("status") == 0) {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                        MyApp.img_url.clear();
                                                                        MyApp.get_url.clear();
                                                                        MyApp.post_url.clear();
                                                                        MyApp.post_url_finish.clear();
                                                                        finish();
                                                                    } else {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }
                                                            }
                                                        });
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }//更新了3张图片
                    else if (MyApp.post_url.size() == 3) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.post_url.get(i).getUrl(), 270, 480, "photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                            photoslist.add("photo" + i + "photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .addFile("hImg3", photoslist.get(2), file3)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 3) {
                                            MyApp.post_url_finish.add(jsonArray.getString(0));
                                            MyApp.post_url_finish.add(jsonArray.getString(1));
                                            MyApp.post_url_finish.add(jsonArray.getString(2));
                                            for (int i = 0; i < MyApp.get_url.size(); i++) {
                                                urls.add(MyApp.get_url.get(i).getUrl());
                                            }
                                            urls.addAll(MyApp.post_url_finish);
                                            if (urls.size() < 4) {
                                                T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                                            } else {
                                                switch (urls.size()) {
                                                    case 4:
                                                        for (int i = 0; i < 2; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                    case 5:
                                                        for (int i = 0; i < 1; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                }
                                                OkHttpUtils.get()
                                                        .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                                        .addParams("id", id)
                                                        .addParams("hImg1", urls.get(0))
                                                        .addParams("hImg2", urls.get(1))
                                                        .addParams("hImg3", urls.get(2))
                                                        .addParams("hImg4", urls.get(3))
                                                        .addParams("hImg5", urls.get(4))
                                                        .addParams("hImg6", urls.get(5))
                                                        .url(API.BaseApi_L + API.UpdateHouseImg)
                                                        .build()
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onError(Request request, Exception e) {
                                                                Log.e("bbbbbbbb", "===" + e);
                                                            }

                                                            @Override
                                                            public void onResponse(String response) {
                                                                try {
                                                                    JSONObject js = new JSONObject(response);
                                                                    if (js.getInt("status") == 0) {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                        MyApp.img_url.clear();
                                                                        MyApp.get_url.clear();
                                                                        MyApp.post_url.clear();
                                                                        MyApp.post_url_finish.clear();
                                                                        finish();
                                                                    } else {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }
                                                            }
                                                        });
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }//更新了4张图片
                    else if (MyApp.post_url.size() == 4) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.post_url.get(i).getUrl(), 270, 480, "photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                            photoslist.add("photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .addFile("hImg3", photoslist.get(2), file3)
                                .addFile("hImg4", photoslist.get(3), file4)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 4) {
                                            MyApp.post_url_finish.add(jsonArray.getString(0));
                                            MyApp.post_url_finish.add(jsonArray.getString(1));
                                            MyApp.post_url_finish.add(jsonArray.getString(2));
                                            MyApp.post_url_finish.add(jsonArray.getString(3));
                                            for (int i = 0; i < MyApp.get_url.size(); i++) {
                                                urls.add(MyApp.get_url.get(i).getUrl());
                                            }
                                            urls.addAll(MyApp.post_url_finish);
                                            if (urls.size() < 4) {
                                                T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                                            } else {
                                                switch (urls.size()) {
                                                    case 4:
                                                        for (int i = 0; i < 2; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                    case 5:
                                                        for (int i = 0; i < 1; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                }
                                                OkHttpUtils.get()
                                                        .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                                        .addParams("id", id)
                                                        .addParams("hImg1", urls.get(0))
                                                        .addParams("hImg2", urls.get(1))
                                                        .addParams("hImg3", urls.get(2))
                                                        .addParams("hImg4", urls.get(3))
                                                        .addParams("hImg5", urls.get(4))
                                                        .addParams("hImg6", urls.get(5))
                                                        .url(API.BaseApi_L + API.UpdateHouseImg)
                                                        .build()
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onError(Request request, Exception e) {
                                                                Log.e("bbbbbbbb", "===" + e);
                                                            }

                                                            @Override
                                                            public void onResponse(String response) {
                                                                try {
                                                                    JSONObject js = new JSONObject(response);
                                                                    if (js.getInt("status") == 0) {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                        MyApp.img_url.clear();
                                                                        MyApp.get_url.clear();
                                                                        MyApp.post_url.clear();
                                                                        MyApp.post_url_finish.clear();
                                                                        finish();
                                                                    } else {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }
                                                            }
                                                        });
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }//更新了5张图片
                    else if (MyApp.post_url.size() == 5) {
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.post_url.get(i).getUrl(), 270, 480, "photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                            photoslist.add("photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
                        File file5 = new File(Environment.getExternalStorageDirectory(), photoslist.get(4));
//                    File file6 = new File(Environment.getExternalStorageDirectory(), photoslist.get(5));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .addFile("hImg3", photoslist.get(2), file3)
                                .addFile("hImg4", photoslist.get(3), file4)
                                .addFile("hImg5", photoslist.get(4), file5)
//                            .addFile("hImg6",photoslist.get(5), file6)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 5) {
                                            MyApp.post_url_finish.add(jsonArray.getString(0));
                                            MyApp.post_url_finish.add(jsonArray.getString(1));
                                            MyApp.post_url_finish.add(jsonArray.getString(2));
                                            MyApp.post_url_finish.add(jsonArray.getString(3));
                                            MyApp.post_url_finish.add(jsonArray.getString(4));
                                            for (int i = 0; i < MyApp.get_url.size(); i++) {
                                                urls.add(MyApp.get_url.get(i).getUrl());
                                            }
                                            urls.addAll(MyApp.post_url_finish);
                                            if (urls.size() < 4) {
                                                T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                                            } else {
                                                switch (urls.size()) {
                                                    case 4:
                                                        for (int i = 0; i < 2; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                    case 5:
                                                        for (int i = 0; i < 1; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                }
                                                OkHttpUtils.get()
                                                        .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                                        .addParams("id", id)
                                                        .addParams("hImg1", urls.get(0))
                                                        .addParams("hImg2", urls.get(1))
                                                        .addParams("hImg3", urls.get(2))
                                                        .addParams("hImg4", urls.get(3))
                                                        .addParams("hImg5", urls.get(4))
                                                        .addParams("hImg6", urls.get(5))
                                                        .url(API.BaseApi_L + API.UpdateHouseImg)
                                                        .build()
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onError(Request request, Exception e) {
                                                                T.showShort(PhotoSelectActivity.this, "网络连接失败");
                                                            }

                                                            @Override
                                                            public void onResponse(String response) {
                                                                try {
                                                                    JSONObject js = new JSONObject(response);
                                                                    if (js.getInt("status") == 0) {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                        MyApp.img_url.clear();
                                                                        MyApp.get_url.clear();
                                                                        MyApp.post_url.clear();
                                                                        MyApp.post_url_finish.clear();
                                                                        finish();
                                                                    } else {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }
                                                            }
                                                        });
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }
                    else if (MyApp.post_url.size() == 6) {
                        //更新了6张图片
                        LoadingDialog.show(this);
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            BitmapUtils.getBitmap(MyApp.post_url.get(i).getUrl(), 270, 480, "photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                            photoslist.add("photo" + i + MyApp.post_url.get(i).getUrl().substring(MyApp.post_url.get(i).getUrl().lastIndexOf("/") + 1));
                        }
                        File file1 = new File(Environment.getExternalStorageDirectory(), photoslist.get(0));
                        File file2 = new File(Environment.getExternalStorageDirectory(), photoslist.get(1));
                        File file3 = new File(Environment.getExternalStorageDirectory(), photoslist.get(2));
                        File file4 = new File(Environment.getExternalStorageDirectory(), photoslist.get(3));
                        File file5 = new File(Environment.getExternalStorageDirectory(), photoslist.get(4));
                        File file6 = new File(Environment.getExternalStorageDirectory(), photoslist.get(5));
                        OkHttpUtils.post()
                                .addParams("token", SPUtils.get(this, "token", "") + "")
                                .addFile("hImg1", photoslist.get(0), file1)
                                .addFile("hImg2", photoslist.get(1), file2)
                                .addFile("hImg3", photoslist.get(2), file3)
                                .addFile("hImg4", photoslist.get(3), file4)
                                .addFile("hImg5", photoslist.get(4), file5)
                                .addFile("hImg6", photoslist.get(5), file6)
                                .url(API.BaseApi_L + API.PostHouseImg).build().execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                T.showShort(PhotoSelectActivity.this, "网络连接失败，请检查一下网络设置");
                            }

                            @Override
                            public void onResponse(String response) {
                                LoadingDialog.dismiss(PhotoSelectActivity.this);
                                Log.e("pppppppp", "=" + response);
                                try {
                                    final JSONObject jsonObject = new JSONObject(response);
                                    if (jsonObject.getInt("status") == 0) {
                                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                                        if (jsonArray.length() == 6) {
                                            MyApp.post_url_finish.add(jsonArray.getString(0));
                                            MyApp.post_url_finish.add(jsonArray.getString(1));
                                            MyApp.post_url_finish.add(jsonArray.getString(2));
                                            MyApp.post_url_finish.add(jsonArray.getString(3));
                                            MyApp.post_url_finish.add(jsonArray.getString(4));
                                            MyApp.post_url_finish.add(jsonArray.getString(5));
                                            for (int i = 0; i < MyApp.get_url.size(); i++) {
                                                urls.add(MyApp.get_url.get(i).getUrl());
                                            }
                                            urls.addAll(MyApp.post_url_finish);
                                            if (urls.size() < 4) {
                                                T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                                            } else {
                                                switch (urls.size()) {
                                                    case 4:
                                                        for (int i = 0; i < 2; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                    case 5:
                                                        for (int i = 0; i < 1; i++) {
                                                            urls.add("");
                                                        }
                                                        break;
                                                }
                                                OkHttpUtils.get()
                                                        .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                                        .addParams("id", id)
                                                        .addParams("hImg1", urls.get(0))
                                                        .addParams("hImg2", urls.get(1))
                                                        .addParams("hImg3", urls.get(2))
                                                        .addParams("hImg4", urls.get(3))
                                                        .addParams("hImg5", urls.get(4))
                                                        .addParams("hImg6", urls.get(5))
                                                        .url(API.BaseApi_L + API.UpdateHouseImg)
                                                        .build()
                                                        .execute(new StringCallback() {
                                                            @Override
                                                            public void onError(Request request, Exception e) {
                                                                T.showShort(PhotoSelectActivity.this, "网络连接失败");
                                                            }

                                                            @Override
                                                            public void onResponse(String response) {
                                                                try {
                                                                    JSONObject  js = new JSONObject(response);
                                                                    if (js.getInt("status") == 0) {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                        MyApp.img_url.clear();
                                                                        MyApp.get_url.clear();
                                                                        MyApp.post_url.clear();
                                                                        MyApp.post_url_finish.clear();
                                                                        finish();
                                                                    }else {
                                                                        T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                                    }
                                                                } catch (JSONException e) {
                                                                    e.printStackTrace();
                                                                }

                                                            }
                                                        });
                                                finish();
                                            }
                                        }
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    } else if (MyApp.post_url.size() == 0) {
                        //没有增加新的图片
                        if (MyApp.get_url.size() < 4) {
                            T.showShort(PhotoSelectActivity.this, "最少上传4张图片");
                        } else {
                            switch (MyApp.get_url.size()) {
                                case 4:
                                    for (int i = 0; i < 2; i++) {
                                        MyApp.get_url.add(new PhotoBean("edit", ""));
                                    }
                                    break;
                                case 5:
                                    for (int i = 0; i < 1; i++) {
                                        MyApp.get_url.add(new PhotoBean("edit", ""));
                                    }
                                    break;
                            }
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(PhotoSelectActivity.this, "token", "") + "")
                                    .addParams("id", id)
                                    .addParams("hImg1", MyApp.get_url.get(0).getUrl())
                                    .addParams("hImg2", MyApp.get_url.get(1).getUrl())
                                    .addParams("hImg3", MyApp.get_url.get(2).getUrl())
                                    .addParams("hImg4", MyApp.get_url.get(3).getUrl())
                                    .addParams("hImg5", MyApp.get_url.get(4).getUrl())
                                    .addParams("hImg6", MyApp.get_url.get(5).getUrl())
                                    .url(API.BaseApi_L + API.UpdateHouseImg)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            T.showShort(PhotoSelectActivity.this,"网络连接失败");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            try {
                                                JSONObject  js = new JSONObject(response);
                                                if (js.getInt("status") == 0) {
                                                    T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                    MyApp.img_url.clear();
                                                    MyApp.get_url.clear();
                                                    MyApp.post_url.clear();
                                                    MyApp.post_url_finish.clear();
                                                    finish();
                                                }else {
                                                    T.showShort(PhotoSelectActivity.this, js.getString("msg"));
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        }

                    }
                }
                break;
        }
    }

    private void UpdateHouseImg(List<String> urls) {
    }

}
